
# # coding='utf-8'
# import requests
# from urllib import parse
# import base64
# import time
# import hashlib
# import random
# from collections import OrderedDict


# App_Key = 'a95eceb1ac8c24ee28b70f7dbba912bf'
# url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_driverlicenseocr'


# def getReqSign(param, App_Key):
#     names = sorted(param.keys())

#     strings = parse.urlencode(param).split('&')

#     out_strings = []
#     for name in names:
#         for string in strings:
#             name_s = string.split('=')[0]

#             if name_s == name:
#                 out_strings.append(string + '&')
#                 break

#     out_strings.append('app_key=' + App_Key)
#     out_string = ''.join(out_strings)
#     print('out_string: ',out_string)
#     h1 = hashlib.md5()
#     h1.update(out_string.encode(encoding="utf-8"))

#     sign = h1.hexdigest().upper()
#     print(sign)
#     return sign


# def doHttpPost(img_path):
#     # file = open(img_path, 'rb').read()
#     # file_bs64 = base64.b64encode(file).decode()
#     #
#     # print(file_bs64)

#     param = {}
#     param['app_id'] = '10000'

#     param['time_stamp'] = '1493449657'

#     param['nonce_str'] = '20e3408a79'.encode('utf-8')

#     param['key1'] = '腾讯AI开放平台'
#     param['key2'] = '示例仅供参考'

#     param['sign'] = getReqSign(param, App_Key)

#     # r = requests.post(url, data=param)
#     # print(r.text)


# if __name__ == '__main__':

#     img_path = r'C:\\Users\\vanlance\\Desktop\\E63382462ED0891C243E903B8A0304AD.jpg'
#     doHttpPost(img_path)

# # coding='utf-8'
# import requests
# from urllib import parse
# import base64
# import time
# import hashlib
# import random
# import json

# app_id = 2109145882
# App_Key = 'HRgRwW18nD5WFJjs'
# url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_driverlicenseocr'
# save_path = r'C:\Users\vanlance\Desktop\dll\out.json'

# def getReqSign(param, App_Key):
#     names = sorted(param.keys())

#     strings = parse.urlencode(param).split('&')

#     out_strings = []
#     for name in names:
#         for string in strings:
#             name_s = string.split('=')[0]

#             if name_s == name:
#                 out_strings.append(string + '&')
#                 break

#     out_strings.append('app_key=' + App_Key)
#     out_string = ''.join(out_strings)

#     h1 = hashlib.md5()
#     h1.update(out_string.encode(encoding="utf-8"))

#     sign = h1.hexdigest().upper()

#     return sign


# def doHttpPost(img_path):
#     file = open(img_path, 'rb').read()
#     file_bs64 = base64.b64encode(file).decode()

#     param = {
#         'app_id': app_id,
#         'image': file_bs64,
#         'type': 0,
#         'time_stamp': int(time.time()),
#         'nonce_str': ''.join([App_Key[random.randint(0, 15)] for _ in range(20)]).lower()
#     }

#     param['sign'] = getReqSign(param, App_Key)

#     r = requests.post(url, data=param)
#     out = json.loads(r.text)
#     if out['ret'] == 0:
#         print('ok')
#         with open(save_path,'w') as wf:
#             wf.write(r.text)
#     print(out)

#     img_path = r'C:\\Users\\vanlance\\Desktop\\20181019153205.jpg'
#     doHttpPost(img_path)


import os, random, requests
import threadpool
import threading
import time
import json
import base64
import numpy as np

filter_dir = r''

# from requests_toolbelt.multipart.encoder import MultipartEncoder
def http_test(path0, url):

    # headers = {
    #     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
    #                   'Chrome/68.0.3440.106 Safari/537.36',
    #     'Referer': url
    #     }
    with open(path0, "rb") as rf:
        file_bytes = rf.read()
    files= {'upload_file': base64.b64encode(file_bytes),
            'file_type': "jpg"
    }

    r = requests.post(url, data=files)
    print(r.text)

    # mutex.acquire(timeout=10)
    # with open('test_out.txt', 'a') as f:
    #     f.write(path0  + '  '+ str(time.time()) + ' '*10 + r.text + '\n')
    #     f.close()
    #     count_all +=1
    # mutex.release()

def mutil_test():
    start = time.time()
    dir_path1 = r'C:\Users\vanlance\Desktop\3'
    dir_path2 = r'C:\Users\vanlance\Desktop\3'
    dir_path3 = r'C:\Users\vanlance\Desktop\3'

    func_var1 = [os.path.join(dir_path1,p) for p in os.listdir(dir_path1)]
    func_var2 = [os.path.join(dir_path2,p) for p in os.listdir(dir_path2)]
    func_var3 = [os.path.join(dir_path3,p) for p in os.listdir(dir_path3)]

    func_var_file = func_var1
    func_var_file = func_var_file.extend(func_var2)
    func_var_file = func_var_file.extend(func_var3)

    # mutex = threading.Lock()
    pool = threadpool.ThreadPool(2)
    
    url = ['http://192.168.31.168:8080/dll/dll','http://192.168.31.168:8081/dll/dll']

    func_var_url = []
    for i in range(len(func_var)):
        if i % 2 == 0:
            func_var_url.append(url[0])
        else:
            func_var_url.append(url[1])


    func_var = [[path,url] for a,b in zip(func_var_file, func_var_url)]
    other = [None for i in range(len(func_var1))]
    requests_p = threadpool.makeRequests(http_test, zip(func_var,other))
    [pool.putRequest(req) for req in requests_p]
    pool.wait()
    print(time.time() - start)

def post_cpp_httplib_test():

    url = 'http://localhost:1234/multipart'
    r = requests.post(url, data={'name1':'how are you'}, timeout=5)
    print(r.text)

    # url = 'http://127.0.0.1:9977/api/run'
    # r = requests.get(url, timeout=5)
    # print(r.text)


    # url = 'http://localhost:1234/multipart'
    # data = {'jsonPath':'how are you',
    #         'x': 100,
    #         'y':200
    #     }
    # r = requests.post(url, data=data, timeout=5)
    # # r.apparent_encoding('utf-8')
    # print(r.text)

if __name__ == "__main__":
    post_cpp_httplib_test()

# 请求头必须包含一个特殊的头信息，类似于Content-Type: multipart/form-data; boundary=${bound}
# 注意，不要设置cookies等其他参数，否则会报错

# 例子/usr/local/python36/bin/python3 /opt/lykchat/test_upload.py
# "{'username':'lykchat','pwd':'123456','type':'img','friendfield':'1','friend':'xxxx','content':'恭喜发财'
# ,'file':'/root/b.jpg'}"
# 等同于curl -F "file=@/root/a" 'http://127.0.0.1/sendmsg?username=lykchat&pwd=123456&type=img&
# friendfield=1&friend=xxxx&content=恭喜发财'





